package top.hkyzf.study.leetcode.tree;

import org.junit.Test;

/**
 * @author 朱峰
 * @date 2022-2-23 11:15
 */
public class 验证二叉搜索树_98 {
    long pre = Long.MIN_VALUE;
    public boolean isValidBST(TreeNode root) {
        return dfs(root);
    }

    boolean dfs(TreeNode root) {
        if (root == null) {
            return true;
        }
        if (!dfs(root.left)) {
            return false;
        }
        if (root.val <= pre) {
            return false;
        }
        pre = root.val;
        return dfs(root.right);
    }

    @Test
    public void testIsValidBST () {
        TreeNode node4 = new TreeNode(7);
        TreeNode node3 = new TreeNode(3);
        TreeNode node2 = new TreeNode(6, node3, node4);
        TreeNode node1 = new TreeNode(4);
        TreeNode root = new TreeNode(5, node1, node2);
        System.out.println(isValidBST(root));
    }
}
